package com.memoir.jpa.service;

import com.memoir.jpa.model.TodoDto;
import com.memoir.jpa.repository.TodoJseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import javax.persistence.metamodel.EntityType;

/**
 * @author ldm
 * @since 2017-11-21
 */
@Service
public class TodoJseService {

    //public Object findOne(Specification spec)
    //public List findAll(Specification spec) {
    //public Page findAll(Specification spec, Pageable pageable) {
    //public List findAll(Specification spec, Sort sort) {
    //public long count(Specification spec)

    @Autowired
    private TodoJseRepository todoJseRepository;


    //参考链接

    //http://blog.csdn.net/yingxiake/article/details/51014223
    //http://developer.51cto.com/art/200911/162722.html
    public Page<TodoDto> findAll(Pageable pageable){

//    //CriteriaBuilder,用来构建CritiaQuery的构建器对象
//    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
//
//    //CriteriaQuery,它包含着查询语句的条件各个部分，比如：select 、from、where、group by、order by等
//    CriteriaQuery<CameraInfoPO> criteriaQuery = criteriaBuilder.createQuery(CameraInfoPO.class);
//
//    //查询根，用于获取查询实例的属性，通过CriteriaQuery的from方法获取
//    Root<CameraInfoPO> rootFrom = criteriaQuery.from(CameraInfoPO.class);
//
//    //查询条件
//    List<Predicate> predicates = new ArrayList<Predicate>();
//
//    if (null != isEnabled) {
//        Predicate predicate = criteriaBuilder.equal(rootFrom.get("isEnabled").as(Integer.class), isEnabled);
//        predicates.add(predicate);
//
//    }
//
//    //格式化参数
//    criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
//
//    //默认按照id排序（从小到大）
//    criteriaQuery.orderBy(criteriaBuilder.asc(rootFrom.get("id")));
//
//    //SQL查询对象
//    TypedQuery<CameraInfoPO> createQuery = em.createQuery(criteriaQuery);
//
//    //分页参数
//    Integer pageSize = pageable.getPageSize();
//    Integer pageNo = pageable.getPageNumber();
//
//    //计数查询结果条数
//    TypedQuery<CameraInfoPO> createCountQuery = em.createQuery(criteriaQuery);
//
//    // 实际查询返回分页对象
//    int startIndex = pageSize * pageNo;
//    createQuery.setFirstResult(startIndex);
//    createQuery.setMaxResults(pageable.getPageSize());
//    Page<CameraInfoPO> pageRst =
//            new PageImpl<CameraInfoPO>(createQuery.getResultList(), pageable, createCountQuery.getResultList().size());

        Page<TodoDto> todoDtoPage = todoJseRepository.findAll(new Specification<TodoDto>() {
            public Predicate toPredicate(Root<TodoDto> todoDtoRoot, CriteriaQuery<?> query, CriteriaBuilder cb) {
//                todoDtoRoot.
                EntityType<TodoDto> model = todoDtoRoot.getModel();

                Path<String> title = todoDtoRoot.get("title");
                Predicate predicate = cb.like(title,"西门庆");

                return predicate;
            }
        }, pageable);

        return todoDtoPage;
    }

}
